package com.example.ota_ble.ble.adv

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log

/**
 *  外部接口registerDeviceSearchListener用于注册事件回调
 * */

open class advRspRecall  : BroadcastReceiver(){
    private var TAG = "rocjan.advRspRecall"
    private lateinit var  onDevSearcher:OnDeviceSearchListener
    companion object{
        interface  OnDeviceSearchListener{
            // 1、搜索层回调
            fun onDiscoveryStart_Recall()   //开启搜索
            fun onDiscoveryStop_Recall()   ///完成搜索
            // 2、
            // 1、搜索到设备
            fun onDiscoveryFound_Recall(bluetoothDevice: BluetoothDevice, rssi: Short?)
            // 2、配对请求
            fun onDevice_pairingRequest_Recall()
            // 3、配对状态回调
            // 1、成功
            fun onDevice_pairingSuccess_Recall(bluetoothDevice: BluetoothDevice)
            // 1、失败
            fun onDevice_pairingFail_Recall(bluetoothDevice: BluetoothDevice)
        }
    }

    fun  registerDeviceSearchListener( DevSearcher:OnDeviceSearchListener)
    {
        this.onDevSearcher = DevSearcher;
    }

    /*
    *   重构BroadcastReceiver的广播接收回调函数
    * */
    override fun onReceive(context: Context?, intent: Intent?) {
        var action:String = intent?.action.toString()

        var  device:BluetoothDevice ?= if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                                    intent?.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)
                                } else {
                                    Log.i(TAG,"VERSION.SDK_INT < TIRAMISU")
                                    intent?.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                }
        // 处理广播事件
        when (action)
        {
            // 1、开始搜索
            BluetoothAdapter.ACTION_DISCOVERY_STARTED ->{
                Log.i(TAG,"搜索设备，准备执行搜索回调")
                if(this.onDevSearcher!=null)
                {
                    this.onDevSearcher.onDiscoveryStart_Recall()
                }
            }
            // 2、搜索结束
            BluetoothAdapter.ACTION_DISCOVERY_FINISHED ->{
                Log.i(TAG,"搜索结束，准备执行搜索结束回调")
                if(this.onDevSearcher!=null)
                {
                    this.onDevSearcher.onDiscoveryStop_Recall()
                }
            }

            // 1、设备层

            // 1、搜索发现到设备
            BluetoothDevice.ACTION_FOUND ->{
                Log.i(TAG,"搜索发现到设备，准备执行搜索发现到设备回调")
                var rssi = intent?.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE)
                if(device!=null && this.onDevSearcher!=null)
                {
                    this.onDevSearcher.onDiscoveryFound_Recall(device,rssi)
                }
            }
            // 1、开始配对
            BluetoothDevice.ACTION_PAIRING_REQUEST ->{
                Log.i(TAG,"配对请求设备，准备执行开始配对请求设备回调")
                if(this.onDevSearcher!=null)
                {
                    this.onDevSearcher.onDevice_pairingRequest_Recall()
                }
            }
            // 2、配对状态
            BluetoothDevice.ACTION_BOND_STATE_CHANGED ->{
                Log.i(TAG,"配对请求状态，准备执行配对状态回调")
                val bondState = intent?.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR)
                when (bondState) {
                    BluetoothDevice.BOND_BONDED -> {
                        Log.i(TAG,"配对请求状态，准备执行配对成功回调")
                        if(device!=null && this.onDevSearcher!=null)
                        {
                            this.onDevSearcher.onDevice_pairingSuccess_Recall(device)
                        }
                    }
                    BluetoothDevice.BOND_NONE -> {
                        Log.i(TAG,"配对请求状态，准备执行配对失败回调")
                        if(device!=null && this.onDevSearcher!=null)
                        {
                            this.onDevSearcher.onDevice_pairingFail_Recall(device)
                        }
                    }
                }

            }
            else->{
                Log.i(TAG,"未知注册")

            }
        }


    }


}